package com.shaw.inappupdate_app.page

import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ThumbUp
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.shaw.inappupdate_app.repository.Updater
import com.shaw.inappupdate_app.repository.getVersionInfo
import com.shaw.inappupdate_app.repository.getVersionInfoLocal
import com.shaw.inappupdate_app.repository.response
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


val apiService = "http://192.168.0.103/version"

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun myapp() {
    val context = LocalContext.current
    val version = getVersionInfoLocal()!!
    val scope = CoroutineScope(Dispatchers.IO)
    var versionInfo = response(success = false, version = version, url = "")
    var showDia by remember { mutableStateOf(false) }

    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("in app update achieve") },
                navigationIcon = {
                    Icon(
                        imageVector = Icons.Default.ThumbUp,
                        contentDescription = "big Tumb"
                    )
                })
        },
        content = { padding ->
            Box(
                modifier = Modifier
                    .padding(padding)
                    .fillMaxSize()
            ) {
                Column(
                    modifier = Modifier.align(Alignment.Center),
                    verticalArrangement = Arrangement.Center
                ) {
                    Text("version Now:$version")
                    Spacer(modifier = Modifier.padding(20.dp))
                    Button(
                        onClick = {
                            Log.d("onClick", "myapp: 发生了一次点击")
                            scope.launch {
                                try {
                                    versionInfo = getVersionInfo(apiService)
                                    Log.d("getVersionInfo", "myapp: $versionInfo")
                                    val versionStr = versionInfo.version
                                    Log.d("let", "myapp: $versionStr")
                                    Log.d("let", "myapp: ${versionInfo.version < version},resp :${versionInfo.version} local $version")
                                    if (versionInfo.version > version) { showDia = true }
                                } catch (e: Exception) {
                                    e.printStackTrace()
                                    withContext(Dispatchers.Main) {
                                        Toast.makeText(context, "update request err:${e.message}", Toast.LENGTH_SHORT).show()
                                    }
                                }
                            }
                        }
                    ) {
                        Text("click me check version")
                    }
                }

                if (showDia) {
                    AlertDialog(
                        onDismissRequest = { showDia = false },
                        title = { Text("Update Available") },
                        text = { Text("A new version of the app is available. Would you like to update?\n $versionInfo") },
                        confirmButton = {
                            TextButton(onClick = {
                                scope.launch {
                                    try {
                                        val update = Updater(context = context, scope = scope)
                                        update.getPerAndDownLoad(url = versionInfo.url)
                                    } catch (e: Exception) {
                                        e.printStackTrace()
                                        Toast.makeText(
                                            context,
                                            "error:${e.message}",
                                            Toast.LENGTH_SHORT
                                        ).show()
                                    }
                                }
                            }) { Text("Download") }
                        },
                        dismissButton = {
                            TextButton(onClick = { showDia = false }) {
                                Text("Later")
                            }
                        }
                    )
                }
            }
        }
    )
}